package com.personalsystem.init;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import net.sf.json.JSONObject;

import com.personalsystem.login.bean.TreeEntity;

public class LoadWebAppData  extends HttpServlet {

	
	private static final long serialVersionUID = 1L; 

	public void init() throws ServletException{ 
		String url=getClass().getClassLoader().getResource("NavigationTree.xml").getPath();
		try {
			List<TreeEntity> list=getList(url);
			System.out.println(list);
			Map map=createMap(1, list);
			JSONObject jsonObject=new JSONObject();
			jsonObject.putAll(map);
			System.out.println(jsonObject.toString());
			this.getServletContext().setAttribute("treeJson", jsonObject.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} 
	
	private Map createMap(int id,List<TreeEntity> list){
		Map m=new TreeMap();
		for(TreeEntity entity:list){
			if(entity.getId()==id){
				m.put("id", entity.getId()+"");
				m.put("text", entity.getName());
				m.put("url", entity.getUrl());
				break;
			}
		}
		boolean isLeaf=true;
		List childList=new ArrayList();
		for(TreeEntity entity:list){
			if(entity.getPid()==id){
				isLeaf=false;
				childList.add(createMap(entity.getId(), list));
			}
		}
		if(isLeaf){
			m.put("leaf", true);
		}else{
			m.put("children", childList);
		}
		return m;
	}
	
	private List<TreeEntity> getList(String urlString)throws Exception{
		FileInputStream fis=new FileInputStream(urlString);
		List<TreeEntity> list = new ArrayList<TreeEntity>();  
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder builder = factory.newDocumentBuilder();  
        Document document = builder.parse(fis);  
        Element element = document.getDocumentElement();  
          
        NodeList nodes = element.getElementsByTagName("node");  
        for(int i=0;i<nodes.getLength();i++){  
            Element nodeElement = (Element) nodes.item(i);  
            TreeEntity entity=new TreeEntity();
            NodeList childNodes = nodeElement.getChildNodes();  
//          System.out.println("*****"+childNodes.getLength()); 
            for(int j=0;j<childNodes.getLength();j++){  
            	Node node=childNodes.item(j);
                if(node.getNodeType()==Node.ELEMENT_NODE){  
                	if(node.getNodeName().equals("id")){
                		entity.setId(Integer.parseInt(node.getTextContent()));
                	}else if(node.getNodeName().equals("pid")){
                		if(!node.getTextContent().equals("")){
                			entity.setPid(Integer.parseInt(node.getTextContent()));
                		}
                	}else if(node.getNodeName().equals("url")){
                		entity.setUrl(node.getTextContent());
                	}else{
                		entity.setName(node.getTextContent());
                	}
                }  
            }//end for j  
            list.add(entity);  
        }//end for i  
        return list;  
	}

}